package com.example.pro1.service.zhao.alipay.impl;

import com.example.pro1.entity.zhao.OrderInfo;
import com.example.pro1.service.zhao.alipay.AliPayService;
import com.example.pro1.service.zhao.table.OrderInfoService;
import org.apache.http.client.methods.HttpPost;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
@Service
public class AliPayServiceImpl implements AliPayService {
    @Resource
    private OrderInfoService orderInfoService;

    @Override
    public Map<String, Object> nativePay(long productId, String charge, long userId,int fee,String num,String beizhu,String addressId)
    {
        OrderInfo orderInfo=orderInfoService.createOderByProductId(productId,charge,userId,fee,num,beizhu,addressId);
        HttpPost httpPost=new HttpPost();
/*        OrderInfo orderInfo = orderInfoService.createOderByProductId(productId,chargeWay,userId);


        HttpPost httpPost = new HttpPost(wxPayConfig.getDomain().concat(WxApiType.NATIVE_PAY.getType()));
        // 请求body参数
        Gson gson = new Gson();
        Map paramsMap = new HashMap();
        paramsMap.put("appid", wxPayConfig.getAppid());//支付id
        paramsMap.put("mchid", wxPayConfig.getMchId());//商户id
        paramsMap.put("description", orderInfo.getTitle());//商品描述
        paramsMap.put("out_trade_no", orderInfo.getOrderNo());//订单编号
        paramsMap.put("notify_url", wxPayConfig.getNotifyDomain().concat(WxNotifyType.NATIVE_NOTIFY.getType()));//公网服务器地址（接受通知）
        Map amountMap = new HashMap();//订单金额
        amountMap.put("total", orderInfo.getTotalFee());//总金额
        amountMap.put("currency", "CNY");//货币类型
        paramsMap.put("amount", amountMap);

        //将参数转换成json字符串
        String jsonParams = gson.toJson(paramsMap);

        log.info("请求参数：" + jsonParams);

        StringEntity entity = new StringEntity(jsonParams,"utf-8");

        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        //完成签名并执行请求
        CloseableHttpResponse response = wxPayClient.execute(httpPost);

        try
        {
            String bodyAsString = EntityUtils.toString(response.getEntity());//响应体
            int statusCode = response.getStatusLine().getStatusCode();//响应状态码
            if (statusCode == 200) { //处理成功
                log.info("成功, 返回结果 = " + bodyAsString);
            } else if (statusCode == 204) { //处理成功，无返回Body
                log.info("成功");
            } else {
                log.info("Native下单失败,响应码 = " + statusCode+ ",返回结果 = " + bodyAsString);
                throw new IOException("request failed");
            }
            //响应结果
            Map<String, String> resultMap = gson.fromJson(bodyAsString, HashMap.class);

            //二维码
            String codeUrl = resultMap.get("code_url");
            //添加二维码
            orderInfo.setCodeUrl(codeUrl);
            orderInfoService.saveCode_url(orderInfo);

            Map<String, Object> map = new HashMap<>();
            map.put("codeUrl", codeUrl);
            map.put("orderNo", orderInfo.getOrderNo());
            return map;
        }
        finally {
            response.close();
        }*/
        return null;
    }
}
